package org.ruoyi.common.dhuman.utils.v1;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.ini4j.Profile;
import org.ini4j.Wini;
import org.java_websocket.WebSocket;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.handshake.ServerHandshake;
import org.ruoyi.common.dhuman.utils.v1.vo.TTSVo;
import org.springframework.util.DigestUtils;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;

/**
 * <p>Name: WebsocketConfig</p>
 * <p>Description: websocket客户端</p>
 * <p>Author: Jelly</p>
 * <p>Date: 2022/4/25 11:34</p>
 **/
public class WS {

    public String appid;

    public String appSecret;

    public String ws;

    public void loadConf() {
        try {
            Wini wini = new Wini(new File("config.ini"));
            Profile.Section section = wini.get("KEY");
            this.appid = section.get("appid");
            this.appSecret = section.get("appsecret");
            this.ws = section.get("ws");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    public String sign(int timestamp) {
        String serial = this.appid + timestamp + this.appSecret;

        String sign = DigestUtils.md5DigestAsHex(serial.getBytes(StandardCharsets.UTF_8));
        return "MD5 " + sign.toUpperCase();
    }

    public void doTTS(String text) {
        TTSVo requestVO = new TTSVo();
        requestVO.setText(text);
        requestVO.setVoice_name("xiaoya");
        ObjectMapper objectMapper = new ObjectMapper();
        int timestamp = (int)(System.currentTimeMillis() / 1000L);
        try {
            String payload = objectMapper.writeValueAsString(requestVO);
            String sign = this.sign(timestamp);
            HashMap<String, String> headers = new HashMap<>();
            headers.put("X-Deepsound-Appid", this.appid);
            headers.put("X-Deepsound-Timestamp", String.valueOf(timestamp));
            headers.put("X-Deepsound-Sign", sign);
            WebSocketClient webSocketClient = this.webSocketClient(this.ws, headers);
            if (webSocketClient != null) {
                webSocketClient.connect();
                while (!webSocketClient.getReadyState().equals(WebSocket.READYSTATE.OPEN)) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("发送消息: " + payload);
                webSocketClient.send(payload);
            } else {
                System.out.println("websocket无法连接至服务器");
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

    public WebSocketClient webSocketClient(String ws, HashMap<String, String> headers) {
        try {
            return new WebSocketClient(new URI(ws), new Draft_6455(), headers, 0) {
                @Override
                public void onOpen(ServerHandshake serverHandshake) {
                    System.out.println("websocket建立成功");
                }

                @Override
                public void onMessage(String s) {
                    System.out.println("收到服务端返回消息: " + s);
                }

                @Override
                public void onClose(int i, String s, boolean b) {
                    System.out.println("websocket连接关闭");
                }

                @Override
                public void onError(Exception e) {
                    System.out.println("发生错误: " + e.getMessage());
                }
            };
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
